import React, { useEffect } from 'react';
import dayjs from 'dayjs';
// const { ipcRenderer} = require('electron')
import NavBar from '../components/NavBar/NavBar';
import { Button } from 'antd-mobile';
import { setInterval } from 'timers/promises';
const DataFetchPage = () => {
  const handleButtonClick = () => {
    // ipcRenderer.send('start-fetching-data',)
    // ipcRenderer.on('data-fetching-complete',(event,message)=>{
    //   console.log(message)//输出数据获取完成
    // })
    window.electron.ipcRenderer.sendMessage('start-fetching-data', []);
  };
  useEffect(() => {
    const unsubscbibe = window.electron.ipcRenderer.on(
      'data-fetching-complete',
      (e: any, data: any) => {
        console.log(e);
        console.log(data);
      },
    );

    return () => {
      unsubscbibe();
    };
  }, []);
  useEffect(() => {
    // 设置目标时间戳，例如今天15点
    const now = dayjs();
    let targetTime = now.hour(15).minute(9).second(0).millisecond(0);
    let tomorrow;

    if (now.isAfter(targetTime)) {
      console.log('加一天');
      tomorrow = targetTime.add(1, 'day');
    } else {
      console.log('fffffffffffff');
    }

    const msUnitTarget = tomorrow ? tomorrow.diff(now) : targetTime.diff(now);
    console.log(targetTime, 'targetTime', tomorrow, msUnitTarget);
    const timeOutId = window.setTimeout(() => {
      console.log('定时执行');
      window.electron.ipcRenderer.sendMessage('start-fetching-data', []);
    }, msUnitTarget);
    return () => clearTimeout(timeOutId);
  }, []); // 空依赖数组意味着这个effect只会在组件挂载时运行一次

  return (
    <div>
      <div>
      <NavBar></NavBar>
            <Button block color="primary" size="large" onClick={handleButtonClick}>
              下载数据到本地
            </Button>
      </div>
      <div>
        122132313
      </div>
    </div>
  );
};

export default DataFetchPage;
